package com.drawquest.gui.panels;

import com.drawquest.data.Comment;
import com.drawquest.data.Comments;
import com.drawquest.gui.components.CommentPartPanel;
import com.drawquest.network.Network;
import com.gooddies.swing.swypepanel.SwitchablePanel;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

@WiringComponent(singleton = true, lazy = true)
public class CommentsPanel extends SwitchablePanel {

    @Wire
    private Network network;
    private JPanel commentsCanvas;

    public CommentsPanel() {
        initComponents();
        commentsCanvas = new JPanel();
        commentsCanvas.setLayout(new BoxLayout(commentsCanvas, BoxLayout.Y_AXIS));
        commentsScrollPane.getViewport().add(commentsCanvas);
        commentsScrollPane.getVerticalScrollBar().setUnitIncrement(20);
        commentsScrollPane.getVerticalScrollBar().setBlockIncrement(50);
        setTitle("Comments");
    }

    @Override
    public void onShow(boolean isBack) {
        if (!isBack) {
            loadComments();
        }
    }

    private void loadComments() {
        commentsCanvas.removeAll();
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                Comments comments = network.getComments();
                for (int i = 0; i < comments.getSize(); i++) {
                    Comment comment = comments.getComment(i);
                    CommentPartPanel part = new CommentPartPanel();

                    part.setAvatarLabel(comment.getUser().getUserName());
                    part.setAvatarPic(comment.getUser().getAvatarUrl());

                    part.setQuestTitle(comment.getQuest().getTitle());
                    part.setQuestImage(comment.getQuest().getGallery());
                    part.setCommentId(comment.getId());
                    part.setComment(comment);
                    publish(part);
                }

                return null;
            }

            @Override
            protected void process(List chunks) {
                for (CommentPartPanel part : (List<CommentPartPanel>) chunks) {
                    commentsCanvas.add(part);
                    commentsCanvas.add(Box.createVerticalStrut(5));
                }

                commentsCanvas.revalidate();
                commentsCanvas.repaint();
            }
        };
        worker.execute();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        footerPanel = new com.drawquest.gui.components.FooterPanel();
        commentsScrollPane = new javax.swing.JScrollPane();

        setBackground(new java.awt.Color(255, 255, 255));

        commentsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        commentsScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(footerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(commentsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(commentsScrollPane)
                    .addComponent(footerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane commentsScrollPane;
    private com.drawquest.gui.components.FooterPanel footerPanel;
    // End of variables declaration//GEN-END:variables
}
